﻿#region MIT license
/*
 Copyright (c) 2009-2010 Joxon Productions <joxonproductions@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JAsyncServerLib
{
    internal static class JByte
    {
        public static void Resize(ref byte[] bytes, UInt64 size)
        {
            var tempReDim = new byte[size];
            if (bytes != null)
                Array.Copy(bytes, tempReDim, System.Math.Min(bytes.Length, tempReDim.Length));
            bytes = tempReDim;
        }

        public static void Resize(ref byte[] bytes, int size)
        {
            var tempReDim = new byte[size];

            if (bytes != null)
                Array.Copy(bytes, tempReDim, System.Math.Min(bytes.Length, tempReDim.Length));
            bytes = tempReDim;
        }

        public static byte[] Copy(ref byte[] src_bytes, int src_offset)
        {
            if (src_bytes.Length - src_offset < 0)
                throw new Exception("Cannot create a new array of bytes with size < 0");

            var tempReDim = new byte[src_bytes.Length - src_offset];

            Array.Copy(src_bytes, src_offset, tempReDim, 0, src_bytes.Length - src_offset); // + 1); // Is the +1 part correct? Can anyone check pls? :)

            return tempReDim;
        }

        public static byte[] Copy(ref byte[] src_bytes, int src_offset, int length)
        {
            if (src_bytes.Length - src_offset < 0)
                throw new Exception("Cannot create a new array of bytes with size < 0");

            var tempReDim = new byte[length];

            if (length > src_bytes.Length - src_offset)
            {
                length = src_bytes.Length - src_offset;
            }

            Array.Copy(src_bytes, src_offset, tempReDim, 0, length);

            return tempReDim;
        }

        public static byte[] ConvertASCII(string myData)
        {
            var encoding = new ASCIIEncoding();

            return encoding.GetBytes(myData);
        }

        public static byte[] ConvertUTF8(string myData)
        {
            var encoding = new UTF8Encoding();

            return encoding.GetBytes(myData);
        }
        public static byte[] ConvertUnicode(string myData)
        {
            var encoding = new UnicodeEncoding();

            return encoding.GetBytes(myData);
        }
    }

    internal static class UnixTime
    {
        public readonly static DateTime UnixTimeStart = new DateTime(1970, 1, 1, 0, 0, 0);

        public static double GetSeconds()
        {
            TimeSpan ts = (DateTime.UtcNow - UnixTimeStart);
            return (int)ts.TotalSeconds;
        }
    }
}
